package com.yanqu.road.dao.impl.activity.xiuxian;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.xiuxian.data.CrossXiuXianPvpCampData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class CrossXiuXianPvpCampDataDaoImpl extends TempDataObjectDao<CrossXiuXianPvpCampData> {

    public static String getCrossXiuXianPvpCampDataSql = "select * from `t_c_xiuxian_pvp_camp_data` where `activityId`=?";

    public Map<Integer, CrossXiuXianPvpCampData> getCrossXiuXianPvpUserDataMap(int activityId) {
        Map<Integer, CrossXiuXianPvpCampData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossXiuXianPvpCampDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossXiuXianPvpCampData parse = parse(rs);
                map.put(parse.getCampId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertCrossXiuXianPvpCampDataSql = "insert into `t_c_xiuxian_pvp_camp_data`(activityId,campId,serverIdList,score,totalScore,majorUserId,minorUserId,majorPlaceId,minorPlaceId,majorMarkTime,minorMarkTime,updateScoreTime,appointTime,totalUseEnergy) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossXiuXianPvpCampData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCampId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getServerIdList())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMajorUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMinorUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMajorPlaceId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMinorPlaceId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMajorMarkTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMinorMarkTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateScoreTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAppointTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalUseEnergy()));
        if (getDb().executeNoneQuery(insertCrossXiuXianPvpCampDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCrossXiuXianPvpCampDataSql = "update `t_c_xiuxian_pvp_camp_data` set serverIdList=?,score=?,totalScore=?,majorUserId=?,minorUserId=?,majorPlaceId=?,minorPlaceId=?,majorMarkTime=?,minorMarkTime=?,updateScoreTime=?,appointTime=?,totalUseEnergy=? where activityId=? and campId=?";

    @Override
    protected boolean updateData(CrossXiuXianPvpCampData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getServerIdList())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMajorUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMinorUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMajorPlaceId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMinorPlaceId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMajorMarkTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMinorMarkTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateScoreTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAppointTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalUseEnergy()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCampId()));
        if (getDb().executeNoneQuery(updateCrossXiuXianPvpCampDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossXiuXianPvpCampData parseData(ResultSet rs) throws SQLException {
        CrossXiuXianPvpCampData data = new CrossXiuXianPvpCampData();
        data.setActivityId(rs.getInt("activityId"));
        data.setCampId(rs.getInt("campId"));
        data.setServerIdList(JSON.parseObject(rs.getString("serverIdList"), new TypeReference<Set<Long>>() {
        }));
        data.setScore(rs.getLong("score"));
        data.setTotalScore(rs.getLong("totalScore"));
        data.setMajorUserId(rs.getLong("majorUserId"));
        data.setMinorUserId(rs.getLong("minorUserId"));
        data.setMajorPlaceId(rs.getInt("majorPlaceId"));
        data.setMinorPlaceId(rs.getInt("minorPlaceId"));
        data.setMajorMarkTime(rs.getLong("majorMarkTime"));
        data.setMinorMarkTime(rs.getLong("minorMarkTime"));
        data.setUpdateScoreTime(rs.getLong("updateScoreTime"));
        data.setTotalUseEnergy(rs.getLong("totalUseEnergy"));
        data.setAppointTime(rs.getLong("appointTime"));
        return data;
    }
}
